package com.sjsu.cmpe239.contentBasedRecomender;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.sjsu.cmpe239.Constants;
import com.sjsu.cmpe239.beans.Movie;
import com.sjsu.cmpe239.connection.ConnectionPool;
import com.sjsu.cmpe239.googleservice.ImageGenerater;

public class ContentBasedRecomender {
	
public List<Movie> getContentbasedRecomendation(int[] usrPref) {
		
		Connection connection = null;
		Statement stmt = null;
		ConnectionPool conPoolObj = ConnectionPool.getConnectionPoolInstance();
		HashMap<String, Object> hmKeyValueMap = new HashMap<String, Object>();
		ResultSet resultSet = null;
		Map<String, Object> mjsonResult = new HashMap<String, Object>();
		
		float [][]corelationMatrix = new float[19][19];
		HashMap<Integer, Object[]> hmMovieCorelation = new HashMap<Integer, Object[]>();
		List <Movie> lTopMovies = new ArrayList<Movie>(); 

		
		try {
			String strStmt = "select * from movies ";
			connection = conPoolObj.getConnection();
			stmt = connection.createStatement();
			resultSet = stmt.executeQuery(strStmt);
			int movieCount = 0;
			while(resultSet.next()) {
				Map<String, Object> mapRow = new HashMap<String, Object>();
				Object[] arrString = null;

				arrString = new Object [24];

				arrString[0] = "";
				arrString[1] = resultSet.getInt(5);
				arrString[2] = resultSet.getInt(6);
				arrString[3] = resultSet.getInt(7);
				arrString[4] = resultSet.getInt(8);
				arrString[5] = resultSet.getInt(9);
				arrString[6] = resultSet.getInt(10);
				arrString[7] = resultSet.getInt(11);
				arrString[8] = resultSet.getInt(12);
				arrString[9] = resultSet.getInt(13);
				arrString[10] = resultSet.getInt(14);
				arrString[11] = resultSet.getInt(15);
				arrString[12] = resultSet.getInt(16);
				arrString[13] = resultSet.getInt(17);
				arrString[14] = resultSet.getInt(18);
				arrString[15] = resultSet.getInt(19);
				arrString[16] = resultSet.getInt(20);
				arrString[17] = resultSet.getInt(21);
				arrString[18] = resultSet.getInt(22);
				arrString[19] = resultSet.getInt(23);
				arrString[20] = resultSet.getInt(1);
				arrString[21] = resultSet.getString(2);
				arrString[22] = resultSet.getDate(3);
				arrString[23] = 0;
				
				for(int i = 2; i < 20; i++) {
					for(int j = i + 1; j < 20; j++) {
						if((Integer)arrString[i] == 1 && (Integer)arrString[j] == 1) {
							corelationMatrix[i - 1][i - 1]++;
							corelationMatrix[i - 1][j - 1]++;
							corelationMatrix[j - 1][i - 1]++;
						}
					}
					if((Integer)arrString[i] == 1) {
						arrString[0] = arrString[0] + "  " + Constants.genre[i - 2] ;
						arrString[23] = ((Integer)arrString[23]) + 1;
					}
				}
				movieCount++;
				hmMovieCorelation.put((Integer)arrString[20], arrString);
			}
			
			for(int i = 1; i < 19; i++) {
				float divisior = corelationMatrix[i][i];
				for(int j = 1; j < 19; j++) {
					if(corelationMatrix[i][i] != 0 )
						corelationMatrix[i][j] /= divisior;
				}
			}
			int cnt = 0;
			Double[] corelationScore = new Double[movieCount];
			
			String strCount = "select * from movierating";
			ResultSet resultSetRating = stmt.executeQuery(strCount);
			List<Float> alList = new ArrayList<Float>();
			movieCount = 0;
			while(resultSetRating.next()) {
				alList.add(resultSetRating.getFloat(2));
				corelationScore[movieCount] = 0.0;
				movieCount++;
			}
			
			int userGenre = 0;
			for(int i = 0; i < 18; i++) {
				if(usrPref[i] == 1) {
					Iterator iterator = hmMovieCorelation.keySet().iterator();
					while(iterator.hasNext()) {
						double score = 0.0;
						Object[] movie = hmMovieCorelation.get(iterator.next());
						cnt++;

						for(int j = 2; j < 20; j++) {
							if((Integer)movie[j] == 1) {
								score += corelationMatrix[i + 1][j - 1];
							}
						}
						corelationScore[(Integer)movie[20] - 1] += score ;
					}
					userGenre++;
				}
			}
			TreeMap<Double, Integer> tmScore =new TreeMap<Double, Integer>();
			for(int i = 0; i < movieCount; i++) {
				tmScore.put(((corelationScore[i] * alList.get(i))), i + 1);
			}
			for(int i = 0; i < 18; i++) {
				//System.out.println(tmScore.lastEntry());
				Double score = tmScore.lastEntry().getKey();
				int id = tmScore.get(score);
				tmScore.remove(score);
				Object[] movie = hmMovieCorelation.get(id);
				System.out.println("id " + id + " Movie " + movie[21]  + " Genre " + " " + movie[0]);
				Movie movieBean = new Movie();
				movieBean.setMovieName(movie[21].toString());
				
				String movieName = movieBean.getMovieName();
				ImageGenerater imggen = new ImageGenerater();
				movieBean.setImageURL(imggen.getURLForMovie(movieName));
				
				movieBean.setMovieId(movie[20].toString());
				movieBean.setDate(movie[22].toString());
				movieBean.setGenres(movie[0].toString());
				lTopMovies.add(movieBean);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}	finally{
			if(stmt!=null){
				try{
					stmt.close();
				}catch(SQLException sqe){
				}
			}
			if(connection!=null){
				conPoolObj.free(connection);
			}
		}
		try {
		} catch(Exception e) {
		}
		
		return lTopMovies;
	}
}
